import { AccountService, ShaohuoPersonService } from 'services/Api'
import { DateFormat } from 'services/Util'

/* 搜索相关
-------------------------- */
export const listSearchMixin = {
  data () {
    return {
      search: '', // 搜索内容
      typeUserList: '',
      vipUserList: '',
      statusUserList: '',
      isWithdraw: '',
      startTime: '',
      endTime: ''
    }
  },
  methods: {
    // 搜索条件改变
    searchCondChange (list) {
      this.search = ''
      this.typeUserList = ''
      this.vipUserList = ''
      this.statusUserList = ''
      this.isWithdraw = ''
      this.startTime = ''
      this.endTime = ''
      list.forEach(i => {
        switch (i.name) {
          case '模糊查询':
            this.search = i.value
            break
          case '用户类型':
            this.typeUserList = i.value
            break
          case 'Vip':
            this.vipUserList = i.value
            break
          case '用户状态':
            this.statusUserList = i.value
            break
          case '能否提现':
            this.isWithdraw = i.value
            break
          case '注册时间':
            this.startTime = DateFormat(i.value[0], 'yyyy-MM-dd hh:mm:ss') || ''
            this.endTime = DateFormat(i.value[1], 'yyyy-MM-dd hh:mm:ss') || ''
        }
      })
      this.getTableData()
    }
  }
}

/* 状态相关（恢复禁用按钮）
-------------------------- */
export const listStatusMixin = {
  data () {
    return {
      status: '' // 状态:1为有效,0为禁用
    }
  },
  computed: {
    // 修改状态参数
    statusParam () {
      return {
        acc_id: this.acc_id,
        uids: this.selectIds,
        status: this.status,
        is_withdraw: ''
      }
    }
  },
  methods: {
    // 改变状态接口
    currentStatusApi () {
      return AccountService.alertUserStatus(this.statusParam)
    }
  }
}

/* 提现状态相关（是否提现按钮）
-------------------------- */
export const listWithdrawMixin = {
  data () {
    return {
      withdraw: '', // 能否提现:0为允许,1为不允许,''不改变
      currentWithdraw: '', // 当前提现操作
      withdrawPopover: false,
      withdrawList: [
        { name: '允许', value: '0' },
        { name: '禁用', value: '1' }
      ]
    }
  },
  computed: {
    // 修改提现状态参数
    withdrawParam () {
      return {
        acc_id: this.acc_id,
        uids: this.selectIds,
        status: '',
        is_withdraw: this.withdraw
      }
    }
  },
  methods: {
    // 用户选择完毕
    withdrawChange () {
      this.withdrawPopover = true
      this.AsyncLoading('设置用户提现状态', this.withdrawApi)
    },
    // 改变提现状态接口
    currentWithdrawApi () {
      return AccountService.alertUserStatus(this.withdrawParam)
    },
    // 批量提现已选的数据(idKey: 行数据中id对应的字段名)
    withdrawSelected (idKey) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
    },
    // 提现接口
    async withdrawApi () {
      const [ err ] = await this.currentWithdrawApi()
      this.withdraw = ''
      if (err) return this.$message.error('操作失败')
      this.$message.success(`修改成功`)
      this.getTableData()
    }
  }
}

/* 修改用户类型(比如成为捎货人或者VIP)
-------------------------- */
export const listTypeMixin = {
  data () {
    return {
      shaohuo: '', // 成为捎货人:-1为变成普通用户,0为不改变,>1为变成先相应的捎货人
      currentShaohuo: '', // 当前选择的捎货类型
      shaohuoType: [],
      typePopover: false
    }
  },
  computed: {
    // 修改用户类型参数
    typeParam () {
      return {
        acc_id: this.acc_id,
        uids: this.selectIds,
        shaohuo: this.shaohuo,
        vip: this.vip
      }
    }
  },
  methods: {
    // 用户选择完毕
    shaohuoTypeChange () {
      this.typePopover = true
      this.AsyncLoading('设置用户类型', this.typeApi)
    },
    // 修改用户类型接口
    currentTypeApi () {
      return AccountService.alertUserType(this.typeParam)
    },
    // 批量修改用户类型已选的数据(idKey: 行数据中id对应的字段名)
    typeSelected (idKey) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.shaohuoType.length || this.shaohuoUserTypeApi()
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
    },
    // 修改用户类型接口
    async typeApi () {
      const [ err ] = await this.currentTypeApi()
      if (err) return this.$message.error('操作失败')
      this.shaohuo = ''
      this.$message.success(`修改成功`)
      this.getTableData()
    },
    // 全部捎货人身份类型(用于下拉框)
    async shaohuoUserTypeApi () {
      const [ err, res ] = await ShaohuoPersonService.shaohuoUserType({ acc_id: this.acc_id })
      if (err) return this.$message.error('获取失败')
      this.shaohuoType = [ { ust_id: '-1', name: '普通用户' }, ...res.list ]
    }
  }
}
